package com.stusy.blog.everythingblogweb.controller;

import com.stusy.blog.everythingbloginterface.bean.MailDto;
import com.stusy.blog.everythingbloginterface.bean.UserInfo;
import com.stusy.blog.everythingbloginterface.service.LoginService;
import com.stusy.blog.everythingbloginterface.service.MailService;
import com.stusy.blog.everythingbloginterface.service.UserInfoService;
import com.stusy.blog.everythingblogweb.util.BaseUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.NumberUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author Xinpei Li
 * @create 2022-02-17 20:36
 */

@Controller
@RequestMapping("/blog")
@Slf4j
public class LoginController {

    @DubboReference
    private LoginService loginService;
    @DubboReference
    private MailService mailService;
    @DubboReference
    private UserInfoService userInfoService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String LOGIN_SYSCODE_KEY="login_syscode_key:%s";

    private static final String SESSION_INFO_SID = "EVERYTHING-BLOG";

    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestBody Map<String,String> map){
        String userName = map.get("userName");
        String passWord = map.get("passWord");
        String randomCode = map.get("randomCode");
        String redisKey =String.format(LOGIN_SYSCODE_KEY, userName);
        String s = stringRedisTemplate.opsForValue().get(redisKey);
        if(Integer.parseInt(randomCode)!=Integer.parseInt(s)){
            return "验证码错误";
        }
        stringRedisTemplate.delete(redisKey);
        boolean login = loginService.login(userName, passWord);
        String sid = createSid(userName);
        if(login){
            return sid;
        }
        return "密码或用户名错误";
    }

    private String createSid(String userName) {
        String sidBefore = System.currentTimeMillis() + "|" + userName + "|" + SESSION_INFO_SID;
        String sid = BaseUtils.encrypt(sidBefore);
        log.info("createSid sid={}",sid);
        return sid;
    }

    @GetMapping("/test")
    @ResponseBody
    public String test(){
        return "测试接口";
    }


    @PostMapping("/send-code")
    @ResponseBody
    public String sendLoginCode(@RequestBody Map<String,String> map ){
        String userName = map.get("userName");
        int randomCode = (int)((Math.random()*9+1)*100000);
        log.info("[sendLoginCode] send random code ={},userName={}",randomCode,userName);
        UserInfo userInfo = userInfoService.getUserInfo(userName);
        String email = userInfo.getEmail();
        String redisKey = String.format(LOGIN_SYSCODE_KEY,userName);
        //使用三参数方法，导致redis-value前面有空格
        stringRedisTemplate.opsForValue().set(redisKey, String.valueOf(randomCode), 5000,TimeUnit.SECONDS);
//        stringRedisTemplate.opsForValue().set(redisKey,);
        String content = String.format("[任用博客] 您的验证码为%s,本次登录验证码5分钟有效。",String.valueOf(randomCode));
        MailDto mailDto = new MailDto(email, content);
        boolean b = mailService.sendMail(mailDto);
        return String.valueOf(b);
    }




}
